/**   
 * @Title: NewFunctionIntroductionAct.java 
 * @Package com.esun.rabbit2.viewer 
 * @Description: TODO(客户端首次运行时新功能介绍) 
 * @author: Billy Qigs@500wan.com   
 * @date: 2011-12-31 上午10:55:31  
 * Copyright: (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.esun.config.Language;
import com.esun.model.introduction.NewFunctionIntroductionListInfo;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: NewFunctionIntroductionAct
 * @Description: TODO(客户端首次运行时新功能介绍)
 * @author: Billy Qigs@500wan.com
 * @date: 2011-12-31 上午10:55:31
 * 
 */
public class NewFunctionIntroductionAct extends Activity implements OnGestureListener, OnTouchListener, OnClickListener {

	private ViewFlipper viewFlipper;
	private GestureDetector gestureDetector;
	private int currentLayoutState;
	private int introductionListCount = 0;
	private int[] introductionList = null;
	private Button btnCancle;
	private Intent intent;
	private RadioGroup rbg;

	private static final int FLING_MIN_DISTANCE = 100;
	private static final int FLING_MIN_VELOCITY = 200;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.new_function_introduction_act);
		EDebug.o(this, "new funciton introduction act is on create");

		initUI();

	}

	private void initUI() {

		intent = new Intent().setClass(NewFunctionIntroductionAct.this, MainAct.class);

		viewFlipper = (ViewFlipper) findViewById(R.id.new_function_introduction_viewflipper_introduct);
		// 注册一个用于手势识别的类
		gestureDetector = new GestureDetector(this);
		// 给mFlipper设置一个listener
		viewFlipper.setOnTouchListener(this);
		currentLayoutState = 0;
		// 允许长按住ViewFlipper,这样才能识别拖动等手势
		viewFlipper.setLongClickable(true);

		introductionList = NewFunctionIntroductionListInfo.getIntroductionListInfo().getIntroductionList();
		introductionListCount = NewFunctionIntroductionListInfo.getIntroductionListInfo().getIntroductionCount();

		for (int i = 0; i < introductionListCount; i++) {

			addFlipperItem(introductionList[i]);

		}

		LinearLayout introduction_end = (LinearLayout) LayoutInflater.from(NewFunctionIntroductionAct.this).inflate(R.layout.introduction_end_picture, null);
		btnCancle = (Button) introduction_end.findViewById(R.id.introduction_end_btn_cancle);
		btnCancle.setOnClickListener(this);

		viewFlipper.addView(introduction_end, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

		rbg = (RadioGroup) findViewById(R.id.new_function_introduction_rbg);
		rbg.setVisibility(View.GONE);
		// 暂时不需要
		// addAllPointer(rbg, viewFlipper.getChildCount());
		// setRadioButtonChecked(0);
		rbg.check(R.id.introduction_rb_1);
	}

	// 设置点指示器第x个被选中
	private void setRadioButtonChecked(int index) {
		// if (index >= 0 || index < viewFlipper.getChildCount()) {
		// rbg.check(index);
		// }
		if (index == 0) {
			rbg.check(R.id.introduction_rb_1);
		} else if (index == 1) {
			rbg.check(R.id.introduction_rb_2);
		} else {
			rbg.check(R.id.introduction_rb_3);
		}
	}

	// 增加所有下方点指示器
	// 这里没有使用，是由于点指示器动态添加不完善，之后修正
	@SuppressWarnings("unused")
	private void addAllPointer(RadioGroup rbg, int count) {
		for (int i = 0; i < count; i++) {
			addOnePointer(rbg, i);
		}
	}

	// 增加下方点指示器
	private void addOnePointer(RadioGroup rbg, int tag) {
		RadioButton rb = new RadioButton(NewFunctionIntroductionAct.this);
		rb.setButtonDrawable(getResources().getDrawable(R.drawable.rb_introduction_style));
		// rb.setBackgroundDrawable(getResources().getDrawable(R.drawable.rb_introduction_style));
		rb.setHeight(10);
		rb.setWidth(10);
		rb.setId(tag);
		rb.setEnabled(false);
		rbg.addView(rb);
	}

	// 增加一张图片
	private void addFlipperItem(int index) {
		ImageView image = new ImageView(NewFunctionIntroductionAct.this);
		image.setBackgroundDrawable(getResources().getDrawable(index));
		viewFlipper.addView(image, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	/*
	 * 用户按下触摸屏、快速移动后松开即触发这个事件 e1：第1个ACTION_DOWN MotionEvent e2：最后一个ACTION_MOVE MotionEvent velocityX：X轴上的移动速度，像素/秒 velocityY：Y轴上的移动速度，像素/秒 触发条件 ：
	 * X轴的坐标位移大于FLING_MIN_DISTANCE，且移动速度大于FLING_MIN_VELOCITY个像素/秒
	 */
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
			// 当像左侧滑动的时候
			if (currentLayoutState < introductionListCount) {
				// 设置View进入屏幕时候使用的动画
				viewFlipper.setInAnimation(inFromRightAnimation());
				// 设置View退出屏幕时候使用的动画
				viewFlipper.setOutAnimation(outToLeftAnimation());
				viewFlipper.showNext();
				currentLayoutState++;
				// 暂时不用
				// setRadioButtonChecked(currentLayoutState);
				// if (currentLayoutState == viewFlipper.getChildCount() - 1) {
				// rbg.setVisibility(View.GONE);
				// } else {
				// rbg.setVisibility(View.VISIBLE);
				// }
				EDebug.o("==onFling==", "mCurrentLayoutState id = " + currentLayoutState);
				EDebug.o("==onFling==", "view id = " + viewFlipper.getCurrentView().getBackground());
			} else {
				Toast.makeText(NewFunctionIntroductionAct.this, Language.TIP_ALREADY_LAST_PAGE, Toast.LENGTH_SHORT).show();
			}
		} else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY) {
			// 当像右侧滑动的时候
			if (currentLayoutState > 0) {
				viewFlipper.setInAnimation(inFromLeftAnimation());
				viewFlipper.setOutAnimation(outToRightAnimation());
				viewFlipper.showPrevious();
				currentLayoutState--;
				// 暂时不用
				// if (currentLayoutState == viewFlipper.getChildCount() - 1) {
				// rbg.setVisibility(View.GONE);
				// } else {
				// rbg.setVisibility(View.VISIBLE);
				// }
				setRadioButtonChecked(currentLayoutState);
				EDebug.o("==onFling==", "mCurrentLayoutState id = " + currentLayoutState);
				EDebug.o("==onFling==", "view id = " + viewFlipper.getCurrentView().getBackground());
			} else {
				Toast.makeText(NewFunctionIntroductionAct.this, Language.TIP_ALREADY_FIRST_PAGE, Toast.LENGTH_SHORT).show();
			}
		}
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// 一定要将触屏事件交给手势识别类去处理（自己处理会很麻烦的）
		return gestureDetector.onTouchEvent(event);
	}

	/**
	 * 此方法在本例中未用到，可以指定跳转到某个页面
	 * 
	 * @param switchTo
	 */
	// public void switchLayoutStateTo(int switchTo) {
	// Log.d("==switchLayoutStateTo==", "switch to " + switchTo);
	// while (mCurrentLayoutState != switchTo) {
	// if (mCurrentLayoutState > switchTo) {
	// mCurrentLayoutState--;
	// mFlipper.setInAnimation(inFromLeftAnimation());
	// mFlipper.setOutAnimation(outToRightAnimation());
	// mFlipper.showPrevious();
	// Log.d("==switchLayoutStateTo==", "view id = " + mFlipper.getCurrentView().getId());
	// setRBChecked(mFlipper.getCurrentView().getId());
	// } else {
	// mCurrentLayoutState++;
	// mFlipper.setInAnimation(inFromRightAnimation());
	// mFlipper.setOutAnimation(outToLeftAnimation());
	// mFlipper.showNext();
	// Log.d("==switchLayoutStateTo==", "view id = " + mFlipper.getCurrentView().getId());
	// setRBChecked(mFlipper.getCurrentView().getId());
	// }
	//
	// }
	// }

	/**
	 * 定义从右侧进入的动画效果
	 * 
	 * @return
	 */
	protected Animation inFromRightAnimation() {
		Animation inFromRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(500);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}

	/**
	 * 定义从左侧退出的动画效果
	 * 
	 * @return
	 */
	protected Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(500);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	/**
	 * 定义从左侧进入的动画效果
	 * 
	 * @return
	 */
	protected Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(500);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	/**
	 * 定义从右侧退出时的动画效果
	 * 
	 * @return
	 */
	protected Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(500);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}

	@Override
	public void onClick(View v) {
		startActivity(intent);
		NewFunctionIntroductionAct.this.finish();

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		EDebug.o(NewFunctionIntroductionAct.this, "on keydown 不处理");

		return false;
	}

}
